<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="用户ID" prop="userId">
        <el-input
          style="width: 150px"
          v-model="queryParams.userId"
          placeholder="请输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户名称" prop="userName">
        <el-input
          style="width: 150px"
          v-model="queryParams.userName"
          placeholder="请输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核状态" prop="state">
        <el-select
          style="width: 120px"
          v-model="queryParams.state"
          placeholder="审核状态"
          clearable
        >
          <el-option
            v-for="dict in dict.type.examine_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="提现时间" prop="dateRange">
        <el-date-picker
          style="max-width: 240px"
          v-model="dateRange"
          type="daterange"
          size="small"
          align="right"
          unlink-panels
          value-format="yyyy-MM-dd"
          format="yyyy-MM-dd"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="withdrawalList">
      <el-table-column label="用户ID" align="center" prop="userId" />
      <el-table-column label="用户名称" align="center" prop="userName" />
      <el-table-column label="提现金额" align="center" prop="amount" />
      <el-table-column label="提现服务费" align="center" prop="serviceCharge" />
      <el-table-column label="提现时间" align="center" prop="createTime" />
      <el-table-column
        label="审核状态"
        align="center"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.examine_status"
            :value="scope.row.state"
          />
        </template>
      </el-table-column>
      <el-table-column label="审核备注" align="center" prop="rejectRemark" />
      <el-table-column label="审核时间" align="center" prop="applyTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.applyTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        fixed="right"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            v-if="scope.row.state == 0"
            @click="handleApply(scope.row)"
            >审核</el-button
          >
          <div v-else>-</div>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog
      title="提现申请审核"
      width="530px"
      :visible.sync="dialogFormVisible"
    >
      <el-form :model="form" label-width="80px" ref="form" :rules="rules">
        <el-form-item label="审核状态" prop="state">
          <el-radio-group v-model="form.state">
            <el-radio
              v-for="dict in storeStateApply"
              :key="dict.value"
              :label="dict.value"
              >{{ dict.label }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
        <el-form-item
          label="驳回原因"
          prop="rejectRemark"
          v-if="form.state == 2"
        >
          <el-input
            style="width: 400px"
            type="textarea"
            placeholder="请输入内容"
            v-model="form.rejectRemark"
            maxlength="50"
            show-word-limit
            rows="4"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitApply" :loading="loadingBtn"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { withdrawalApproval, withdrawalPage } from "@/api/store/store";
export default {
  name: "Store",
  dicts: ["examine_status"],
  data() {
    return {
      dialogFormVisible: false,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      withdrawalList: [],
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        userId: "",
        userName: "",
        state: "",
        beginTime: "",
        endTime: "",
      },
      // 日期范围
      dateRange: [],
      form: {
        id: "", //审核id
        state: "",
        rejectRemark: "",
      },
      storeStateApply: [
        { value: 1, label: "通过" },
        { value: 2, label: "驳回" },
      ],
      rules: {
        state: [
          { required: true, message: "请选择审核状态", trigger: "change" },
        ],
        rejectRemark: [
          { required: true, message: "请填写驳回原因", trigger: "blur" },
        ],
      },
      loadingBtn: false,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询店铺列表 */
    getList() {
      this.loading = true;
      if (!Array.isArray(this.dateRange)) {
        this.dateRange = [];
      }
      this.queryParams.beginTime = this.dateRange[0];
      this.queryParams.endTime = this.dateRange[1];
      withdrawalPage(this.queryParams).then((response) => {
        this.withdrawalList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    //审核
    handleApply(item) {
      this.form.id = item.id;
      this.dialogFormVisible = true;
    },
    //确认审核
    submitApply() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.loadingBtn = true;
          withdrawalApproval(this.form).then((res) => {
            if (res.code === 200) {
              this.$message.success("操作成功");
              this.resetForm("form");
              this.dialogFormVisible = false;
              this.getList();
            } else {
              this.resetForm("form");
              this.$message.error(res.msg);
            }
            this.loadingBtn = false;
          });
        }
      });
    },
    cancel() {
      this.resetForm("form");
      this.dialogFormVisible = false;
    },
  },
};
</script>
<style lang="scss" scoped></style>
